package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Button;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.ui.fragment.ReceivedGiftFragment;
import com.yuntongxun.ecdemo.voicechat.ui.fragment.SentGiftFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 礼物
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/12/6
 */

public class GiftsActivity extends BaseVoiceChatActivity {


    @InjectView(R.id.received_btn)
    Button receivedBtn;
    @InjectView(R.id.sent_btn)
    Button sentBtn;
    @InjectView(R.id.viewpager)
    ViewPager mPager;

    private GiftFragmentAdapter mAdapter;

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_gifts;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Gifts");
        mPager.setOffscreenPageLimit(4);
        mAdapter = new GiftFragmentAdapter(getSupportFragmentManager());
        mPager.setAdapter(mAdapter);

        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position==0){
                    receivedBtn.setTextColor(getResources().getColor(R.color.white));
                    receivedBtn.setBackgroundResource(R.color.voice_green);
                    sentBtn.setTextColor(getResources().getColor(R.color.voice_green));
                    sentBtn.setBackgroundResource(R.color.white);
                }else if (position==1){
                    sentBtn.setTextColor(getResources().getColor(R.color.white));
                    sentBtn.setBackgroundResource(R.color.voice_green);
                    receivedBtn.setTextColor(getResources().getColor(R.color.voice_green));
                    receivedBtn.setBackgroundResource(R.color.white);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class GiftFragmentAdapter extends FragmentPagerAdapter {

        private final String[] tabCount = new String[] { "A", "B"};
        private int mCount = tabCount.length;

        public GiftFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
//            return HomeFragment.newInstance(tabCount[position % tabCount.length]);
            if (position==0){
                return ReceivedGiftFragment.newInstance("A");
            }else if (position==1){
                return SentGiftFragment.newInstance("B");
            }
            return null;
        }

        @Override
        public int getCount() {
            return mCount;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabCount[position % tabCount.length];
        }

    }

    @OnClick(R.id.received_btn)
    public void onReceivedClick(){
        receivedBtn.setTextColor(getResources().getColor(R.color.white));
        receivedBtn.setBackgroundResource(R.color.voice_green);
        sentBtn.setTextColor(getResources().getColor(R.color.voice_green));
        sentBtn.setBackgroundResource(R.color.white);
        mPager.setCurrentItem(0);

    }

    @OnClick(R.id.sent_btn)
    public void onSentClick(){
        sentBtn.setTextColor(getResources().getColor(R.color.white));
        sentBtn.setBackgroundResource(R.color.voice_green);
        receivedBtn.setTextColor(getResources().getColor(R.color.voice_green));
        receivedBtn.setBackgroundResource(R.color.white);
        mPager.setCurrentItem(1);
    }

}
